import { request } from "../request/index.js"
import { baseUrl } from "../request/baseUrl.js";

// Promise 形式 getSetting
export const getSetting = () => {
    return new Promise((resolve, reject) => {
        wx.getSetting({
            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            },
            complete: () => { }
        });
    })
}

// Promise 形式 chooseAddress
// export const chooseAddress = () => {
//     return new Promise((resolve, reject) => {
//         wx.chooseAddress({
//             success: (result) => {
//                 resolve(result);
//             },
//             fail: (err) => {
//                 reject(err);
//             },
//             complete: () => { }
//         });
//     })
// }
// Promise 形式 openSetting
export const openSetting = () => {
    return new Promise((resolve, reject) => {
        wx.openSetting({
            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            },
            complete: () => { }
        });
    })
}

/**
 * Promise 形式 showModal
 * @param {object} param0 参数
 */

export const showModal = ({ content }) => {
    return new Promise((resolve, reject) => {
        wx.showModal({
            title: '提示',
            content: content,
            success: (res) => {
                resolve(res);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

/**
 * Promise 形式 showToast
 * @param {object} param0 参数
 */
export const showToast = ({ title }) => {
    return new Promise((resolve, reject) => {
        wx.showToast({
            title: title,
            duration: 1500,
            icon: 'none',
            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            }
        });
    })
}

/**
 * Promise 形式 navigateTo
 * @param {object} param0 参数
 */
export const navigateTo = ({ url }) => {
    return new Promise((resolve, reject) => {
        wx.navigateTo({
            url: url,
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            }
        });
    })
}

/**
 * Promise 形式 login
 */
export const login = () => {
    return new Promise((resolve, reject) => {
        wx.login({
            timeout: 10000,
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            }
        });
    })
}

/**
 * Promise 形式的  小程序的微信支付
 * @param {object} pay 支付所必要的参数
 */
export const requestPayment = (pay) => {
    return new Promise((resolve, reject) => {
        wx.requestPayment({
            ...pay,
            success: (result) => {
                resolve(result);
            },
            fail: (err) => {
                reject(err);
            }
        });

    })
}

export const showShareMenu = () => {
    return new Promise((resolve, reject) => {
        wx.showShareMenu({
            withShareTicket: true,
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

export const getShareInfo = (shareTicket) => {
    return new Promise((resolve, reject) => {
        wx.getShareInfo({
            shareTicket,
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

// // 从缓存获取openid,如果没有则登录获取,结果通过cb函数的参数获取
// export const getOpenid = async (cb) => {
//     let openid = wx.getStorageSync('openid')
//     // 如果没有openid信息，则登录获取
//     if (openid == null) {
//         let { code } = await login()
//         res = await request({ url: "/WxUser/login", data: { code } })
//         wx.setStorageSync('openid', res.data.data.openid)
//         openid = res.data.data.openid
//     }
//     // 将得到的openid传出去
//     typeof cb == "function" && cb(openid)
// }

// 从缓存获取openid,如果没有则登录获取,结果通过cb函数的参数获取  @Deprecated
export const getOpenid = async (cb) => {
    let openid = wx.getStorageSync('openid')
    // 如果没有openid信息，则登录获取
    let err = ""
    let res = ""
    if (openid == null) {
        try {
            let { code } = await login()
            res = await request({ url: "/WxUser/login", data: { code } })
            wx.setStorageSync('openid', res.data.data.openid)
            console.log("getOpenid>>>>>> ", res)
            openid = res.data.data.openid
        } catch (error) {
            err = error
        }
    }
    // 将得到的openid传出去
    typeof cb == "function" && cb(res, err)
}

// 封装带有回调函数的getOpenid  @Deprecated
export const loginPlus = () => {
    return new Promise((resolve, reject) => {
        getOpenid((res, err) => {
            if (res.data.data != null) {
                resolve(res.data.data.openid)
            } else {
                reject(err)
            }
        })
    })
}

// 开始搜索Beacon
export const startBeaconDiscovery = () => {
    return new Promise((resolve, reject) => {
        wx.startBeaconDiscovery({
            uuids : ["fda50693-a4e2-4fb1-afcf-c6eb07647825"],
            success: (result) => {
                resolve(result)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}


// 上传文件
export const uploadFile = (params) => {
    return new Promise((resolve,reject)=>{
        wx.uploadFile({
            ...params,
            name: 'file',
            success: result=>{
                resolve(result)
            },
            fail: err =>{
                reject(err)
            }
        })
    })
}

// 压缩图片
export const compressImage = ({src}) => {
    return new Promise((resolve,reject)=>{
        wx.compressImage({
            src,
            quality: 50,
            success: result=>{
                resolve(result)
            },
            fail: err=>{
                reject(err)
            }
        })
    })
}

// 下载文件
export const downloadFile = (params) =>{

    let header = {...params.header};
    header["Authorization"] = wx.getStorageSync("token");

    return new Promise((resolve,reject) => {
        wx.downloadFile({
            url: baseUrl + params.url,
            header: header,
            success: result => {
                resolve(result)
            },
            fail: err => {
                reject(err)
            }
        })
    })
}

// 打开文件
export const openDocument = (params) => {
    return new Promise((resolve,reject)=>{
        wx.openDocument({
            ...params,
            success: (result) => {
                resolve(result)
            },
            fail: err => {
                reject(err)
            }
        })
    })
}